package com.example.demo.controller;

import com.example.demo.common.domain.User;
import com.example.demo.common.utils.SendCode;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private SendCode sendCode;
    @RequestMapping("/toIndex")
    public  String ToIndex(){
        return "index";}

    @RequestMapping("/toRejister")
    public  String ToRejister(){
        return "rejister";}

    @RequestMapping("/toLogin")
    public  String ToLogin(){
        return "login";}
    @RequestMapping("/toforgetPwd")
    public  String ToForgetPwd(){
        return "forgetPwd";}
    @RequestMapping("/toCheckMsg")
    public  String ToCheckMsg(Model model,HttpSession session){
        String Uname=(String) session.getAttribute("LoginUser");
        User user=userService.SelectUserMsg(Uname);
        model.addAttribute("user",user);
        return "checkMsg";}
   @RequestMapping("/rejister")
    public String Rejister(User user, Model model, @ModelAttribute("code") String Code,HttpSession session){
       System.out.println("注册时："+Code);
       String Firstcode=(String) session.getAttribute("Firstcode");
       System.out.println("第一次："+Firstcode);
       String message=userService.register(user,Code,Firstcode);
       session.setAttribute("Firstcode","");
    if (message.equals("正确")){
        return "login";
    }else {
        model.addAttribute("rejisterMessage",message);
        return "rejister";
    }
   }
   @RequestMapping("/login")
   public String Login( User user ,Model model,HttpSession session){
        String  Uname=user.getuName();
        String  Upw=user.getuPwd();
        System.out.println("密码+用户名:"+Uname+";"+Upw);
        String message=userService.login(user);
         System.out.println(message);
       if (message.equals("正确")){
           session.setAttribute("LoginUser",Uname);
           return "index";
       }else {
           model.addAttribute("loginMessage",message);
           return "login";
       }
   }
    @RequestMapping("/forgetPwd")
    @ResponseBody
    public String ForgetPwd(User user,@ModelAttribute("code") String Code,HttpSession session){

        String Firstcode=(String) session.getAttribute("Firstcode");
        System.out.println("第一次："+Firstcode);
        System.out.println(user.getuName()+";"+user.getuPwd()+";"+Code);
        String message=userService.forgetPwd(user,Code,Firstcode);
        session.setAttribute("Firstcode","");
        System.out.println(message);
        return message;
    }
    @RequestMapping("/checkMsg")
    @ResponseBody
    public String CheckMsg(User user){
            String message=userService.UpdateUserMsg(user);
        return message;
    }


   @RequestMapping("/sendCode")
   @ResponseBody
    public String sendCode(String email,HttpSession session){
    String Code=sendCode.getRandomString();
    session.setAttribute("Firstcode",Code);
       sendCode.sendMessages(Code,email);
    return Code;
    }
}
